package main

import (
	"fmt"
	"mon-go/src/cmd"
	"os"
)

type Command interface {
	Name() string
	Init([]string) error
	Check() error
	Run() error
	Usage()
}

func main() {
	commands := map[string]Command{
		"-kill":  cmd.NewKillCommand(),
		"-start": cmd.NewStartCommand(),
	}
	if len(os.Args) < 2 {
		printUsage(commands)
		os.Exit(1)
	}
	fmt.Printf("Exec command: %s\n", os.Args[1:])
	subcommand := os.Args[1]
	command, exists := commands[subcommand]
	if !exists {
		fmt.Printf("Unknown command: %s\n\n", subcommand)
		printUsage(commands)
		os.Exit(1)
	}
	if err := command.Init(os.Args[2:]); err != nil {
		fmt.Printf("Error initializing command %s: %v\n\n", subcommand, err)
		command.Usage()
		os.Exit(1)
	}
	if err := command.Check(); err != nil {
		fmt.Printf("Fail check %v\n", err)
		os.Exit(1)
	}
	if err := command.Run(); err != nil {
		fmt.Printf("Error running command %s: %v\n", subcommand, err)
		os.Exit(1)
	}
}

func printUsage(commands map[string]Command) {
	fmt.Printf("Usage: %s <command> [options]\n\n", os.Args[0])
	fmt.Println("Available commands:")
	fmt.Println("  kill   - Kill process by port or PID")
	fmt.Println("  start  - Start a program")
	fmt.Printf("\nUse \"%s <command> -h\" for more information about a command\n", os.Args[0])
}
